//account setting submit
function account_submit()
{
    var email = $('#email').val();
    var password = $('#password').val();
    var repassword = $('#repassword').val();
    if (is_email(email) == false) {
	alert("email not valid.");
	return false;
    }
    if (password.length < 6 || repassword.length < 6) {
	alert('too short');
	return false;
    }
    if (password != repassword) {
	alert("password != repassword");
	return false;
    }
    var data = {
	email: email,
	password: password,
	repassword: repassword
    };
    $.ajax({
	'type' : 'POST',
	'data' : data,
	'dataType' : 'json',
	'url'  : '/user/doaccount',
	success : function(msg){
	    if (msg.status == 0) {
		alert("Update Success");
            $('#email').val("");
            $('#password').val("");
            $('#repassword').val("");
	    }
	    else {
		alert(msg.content);
	    }

	},
	error : function(xhr) {
	    alert("Exception");
	}
    
    });
    return false;
}

//submit basic photo
function basic_submit()
{
    var button_id = $(this).attr("id");
    var firstname = $("#firstname").val();
    var lastname  = $("#lastname").val();
    if (is_name(firstname) == false) {
	alert("firstname not valid.");
	return false;
    }
    if (is_name(lastname) == false) {
	alert("lastname not valid.");
	return false;
    }

    var area = $("#area").val();
    var gender = $("#gender").val();
    var data = {
	firstname : firstname,
	lastname  : lastname,
	area: area,
	gender : gender,
    };
    $.ajax({
	'type' : 'POST',
	'data' : data,
	'dataType' : 'json',
	'url'  : '/user/dobasic',
	success : function(msg){
	    if (msg.status == 0) {
		alert("Update Success");
		if (button_id == "save_continue") {
		    location.href = next_url;
		}
	    }
	    else {
		alert(msg.content);
	    }

	},
	error : function(xhr) {
	    alert("Exception");
	}
    
    });
    return false;
}

//gender_list_ctrl
function gender_list_ctrl()
{
    $("#gender_list").show();
    return false;
}

function gender_list_elemt()
{
    $("#gender_name").val($(this).text());
    $("#gender").val($(this).attr("id"));
    $("#gender_list").hide();
}

//age_list_ctrl
function age_list_ctrl()
{
    $("#age_list").show();
    return false;
}

function age_list_elemt()
{
    $("#age_name").val($(this).text());
    $("#age_id").val($(this).attr("id"));
    $("#age_list").hide();
}

//body_list_ctrl
function body_list_ctrl()
{
    $("#body_list").show();
    return false;
}

function body_list_elemt()
{
    $("#body_name").val($(this).text());
    $("#body_id").val($(this).attr("id"));
    $("#body_list").hide();
}

//area_list_ctrl
function area_list_ctrl()
{
    $("#area_list").show();
    return false;
}

function area_list_elemt()
{
    $("#area_name").val($(this).text());
    $("#area").val($(this).attr("id"));
    $("#area_list").hide();
}
//experience_list_ctrl
function experience_list_ctrl()
{
    $("#experience_list").show();
    return false;
}

function experience_list_elemt()
{
    $("#experience_name").val($(this).text());
    $("#experience_id").val($(this).attr("id"));
    $("#experience_list").hide();
}
//trainingservice_list_ctrl
function trainingservice_list_ctrl()
{
    $("#trainingservice_list").show();
    return false;
}

function trainingservice_list_elemt()
{
    $("#trainingservice_name").val($(this).text());
    $("#trainingservice_id").val($(this).attr("id"));
    $("#trainingservice_list").hide();
}
//edulevel_list_ctrl
function edulevel_list_ctrl()
{
    $("#edulevel_list").show();
    return false;
}

function edulevel_list_elemt()
{
    $("#edulevel_name").val($(this).text());
    $("#edulevel_id").val($(this).attr("id"));
    $("#edulevel_list").hide();
}

//ajax upload photo file
function upload_file()
{
    var file = $("#photo").val();
    if (file == "") {
	alert("Please choose file.");
	return false;
    }
    $.ajaxFileUpload({
	url:'/user/ajaxuploadfile',
	secureuri:false,
	fileElementId:'photo',
	dataType: 'json',
	beforeSend:function() {},
	complete:function(){},				
	success: function (data, status)
	{
	    if (data.status != 0) {
		alert(data.content);
		return;
	    }
	    $("#image_photo").attr("src", data.content);
	    $("#photo").val("");
	},
	error: function (data, status, e)
	{
	    alert(e);
	}
    });
}

//delete photo file
function delete_file()
{
    $.ajax({
	'type' : 'POST',
	'dataType' : 'json',
	'url'  : '/user/deletephoto',
	success : function(msg){
	    if (msg.status == 0) {
		$("#image_photo").attr("src", msg.content);
	    }
	    else {
		alert(msg.content);
	    }

	},
	error : function(xhr) {
	    alert("Exception");
	}
    
    });
}

//get the gen_goals
function get_gen_goals(id)
{
    var data = "";
    $("#" + id).find("label").each(function(){if ($(this).attr("class") == "check-box check-true"){data = data + "#" + $(this).attr("id");}});
    return data;
}

function array_exists(arr, val)
{
    for (var i = 0; i < arr.length; ++i) {
	if (arr[i] == val) {
	    return true;
	}
    }
    return false;
}
//add the interested_activity
function add_interested_activity()
{
    var activity = $("#interested_activity").val(); 
    if (activity == "")return;
    if (activity.length > 32) {
	alert("tag too long.");
	return;
    }
    var cnt = 0;
    var arr = new Array(32);

    $("#interested_activities").find("span").each(function(){arr[cnt++]=$(this).text();});
    if (cnt > 15) {
	alert("tag is too much.");
	return;
    }
    var activity_list = activity.split(",");
    var elemt = "";
    for(var i = 0; i < activity_list.length; ++i) {
	if (activity_list[i] == "") {
	    continue;
	}
	if (array_exists(arr, activity_list[i])) {
	    alert(activity_list[i] + " already added before");
	    continue;
	}
	elemt = "<span>" + activity_list[i] + "<i></i></span>";
	$("#interested_activities").append(elemt);
	cnt += 1;
	if (cnt > 15) {
	    $("#interested_activity").val(""); 
	    $("#interested_activities").find("i").each(function(){$(this).click(remove_interested_activity)});
	    alert("tag is too much.");
	    return;
	}
    }
    $("#interested_activity").val(""); 
    $("#interested_activities").find("i").each(function(){$(this).click(remove_interested_activity)});
}
//remove the interested_activity
function remove_interested_activity()
{
    $(this).parent().remove();
    //get_interested_activities();
}
//get the 
function get_interested_activities()
{
    var data = "";
    $("#interested_activities").find("span").each(function(){data += "#" + $(this).text()});
    return data;
}

//submit the client_profile form
function client_profile_submit()
{
    var gen_goals = get_gen_goals("gen_goal_table"); 
    var interested_activities = get_interested_activities();
    var age_id = $("#age_id").val();
    var body_id= $("#body_id").val();

    if (gen_goals == "") {
	alert("general goals is empty.");
	return false;
    }
    if (interested_activities == "") {
	alert("training activity is empty");	
	return false;
    }
    if (age_id == 0) {
	alert("Please select the age.");
	return false;
    }
    if (body_id == 0) {
	alert("Please select the body type.");
	return false;
    }
    var data = {
	gen_goals : gen_goals,
	interested_activities : interested_activities,
	age_id : age_id,
	body_id : body_id
    };
    $.ajax({
	'type' : 'POST',
	'data' : data,
	'dataType' : 'json',
	'url'  : '/user/doclientprofile',
	success : function(msg){
	    if (msg.status == 0) {
		alert("Update Success");
	    }
	    else {
		alert(msg.content);
	    }

	},
	error : function(xhr) {
	    alert("Exception");
	}
    
    });
    return false;
}

//submit the trainer_profile form
function trainer_profile_submit()
{
    var experience_id = $("#experience_id").val();
    var credentials = get_gen_goals("credential_table"); 
    var interested_activities = get_interested_activities();
    var age_id = $("#age_id").val();
    var body_id= $("#body_id").val();
    var trainingservice_id = $("#trainingservice_id").val();
    var edulevel_id = $("#edulevel_id").val();
    var school = $("#school").val();
    var major  = $("#major").val();
    var bio    = $("#bio").val();
    if (experience_id == 0) {
	alert("Experience is empty.");
	return false;
    }
    if (trainingservice_id == 0) {
	alert("Service I provide is empty.");
	return false;
    }
    if (interested_activities == "") {
	alert("training activity is empty");	
	return false;
    }
    if (credentials == "") {
	alert("credential is empty.");
	return false;
    }
    if (edulevel_id == 0 || school == "" || major == "") {
	alert("Education is empty.");
	return false;
    }
    if (school.length > 64 || major.length > 64) {
	alert("school or major is too long.");
	return false;
    }
    if (bio == "") {
	alert("bio is empty.");
	return false;
    }
    if (bio.length > 512) {
	alert("BIO is too long.");
	return false;
    }
    if (age_id == 0) {
	alert("Please select the age.");
	return false;
    }
    if (body_id == 0) {
	alert("Please select the body type.");
	return false;
    }
    var data = {
	credentials : credentials,
	interested_activities : interested_activities,
	trainingservice_id : trainingservice_id,
	edulevel_id : edulevel_id,
	school : school,
	major  : major,
	bio    : bio,
	age_id : age_id,
	body_id : body_id,
	experience_id : experience_id
    };
    $.ajax({
	'type' : 'POST',
	'data' : data,
	'dataType' : 'json',
	'url'  : '/user/dotrainerprofile',
	success : function(msg){
	    if (msg.status == 0) {
		alert("Update Success");
	    }
	    else {
		alert(msg.content);
	    }

	},
	error : function(xhr) {
	    alert("Exception");
	}
    
    });
    return false;
}



$(function(){
    $("#account_form").submit(account_submit);
    $("#save").click(basic_submit);
    $("#save_continue").click(basic_submit);
    $("#client_profile").click(client_profile_submit);
    $("#trainer_profile").click(trainer_profile_submit);

    $("#gender_list_ctrl").click(gender_list_ctrl);
    $("#gender_name").click(gender_list_ctrl);
    $("#gender_block").mouseleave(function(){$("#gender_list").hide();});
    $("#gender_list").find("li").each(function(){$(this).click(gender_list_elemt);});

    $("#age_list_ctrl").click(age_list_ctrl);
    $("#age_name").click(age_list_ctrl);
    $("#age_block").mouseleave(function(){$("#age_list").hide();});
    $("#age_list").find("li").each(function(){$(this).click(age_list_elemt);});

    $("#body_list_ctrl").click(body_list_ctrl);
    $("#body_name").click(body_list_ctrl);
    $("#body_block").mouseleave(function(){$("#body_list").hide();});
    $("#body_list").find("li").each(function(){$(this).click(body_list_elemt);});

    $("#experience_list_ctrl").click(experience_list_ctrl);
    $("#experience_name").click(experience_list_ctrl);
    $("#experience_block").mouseleave(function(){$("#experience_list").hide();});
    $("#experience_list").find("li").each(function(){$(this).click(experience_list_elemt)});

    $("#trainingservice_list_ctrl").click(trainingservice_list_ctrl);
    $("#trainingservice_name").click(trainingservice_list_ctrl);
    $("#trainingservice_block").mouseleave(function(){$("#trainingservice_list").hide();});
    $("#trainingservice_list").find("li").each(function(){$(this).click(trainingservice_list_elemt)});

    $("#edulevel_list_ctrl").click(edulevel_list_ctrl);
    $("#edulevel_name").click(edulevel_list_ctrl);
    $("#edulevel_block").mouseleave(function(){$("#edulevel_list").hide();});
    $("#edulevel_list").find("li").each(function(){$(this).click(edulevel_list_elemt)});

    $("#area_list_ctrl").click(area_list_ctrl);
    $("#area_name").click(area_list_ctrl);
    $("#area_block").mouseleave(function(){$("#area_list").hide();});
    $("#area_list").find("li").each(function(){$(this).click(area_list_elemt)});

    //ajax file upload
    //$("#choose_button").click(function(){$("#photo").show().focus().click();});   
    $("#upload_button").click(upload_file);
    $("#delete_button").click(delete_file);
    
    //label checkbox
    //$("#gen_goal_table").find("label").each(function(){$(this).click(gen_goals_checkbox)});
    //$("#credential_table").find("label").each(function(){$(this).click(gen_goals_checkbox)});
    //add interested_activity
    $("#activity_add").click(add_interested_activity);
    //remove interested_activity
    $("#interested_activities").find("i").each(function(){$(this).click(remove_interested_activity)});

    //add enter event for the activities
    $("#interested_activity").bind('keyup', function(event){
	if (event.keyCode == 13) {
	    add_interested_activity();
	}	
    });
    //account edit line 
    var RightHeight = $("#my-account .right-box").height();
    $("#my-account .left-box").height(RightHeight);
    ////////////////////////////////////

});
